package org.kleathome.dcpu16.asm;

import org.kleathome.dcpu16.DCPU16Address;
import org.kleathome.dcpu16.DCPU16Peek;
import org.kleathome.dcpu16.DCPU16Pop;
import org.kleathome.dcpu16.DCPU16Push;
import org.kleathome.dcpu16.emulator.Emulator;

public class ASMAddress extends DCPU16Address
{
	public ASMAddress(String address)
	{
		super((char) 0xFFFF);
		
		useRegister = true;
		if (Emulator.Registers.isRegister(address))
		{
			_register = Emulator.Registers.getRegister(address);
			_value = (char)(_register.ordinal() + 0x08);
		}
		else if (address.equalsIgnoreCase("push"))
		{
			_instruction = new DCPU16Push();
			_value = 0x1A;
		}
		else if (address.equalsIgnoreCase("pop"))
		{
			_instruction = new DCPU16Pop();
			_value = 0x18;
		}
		else if (address.equalsIgnoreCase("peek"))
		{
			_instruction = new DCPU16Peek();
			_value = 0x19;
		}
		else
		{
			int base = 10;
			_value = 0x1E;
			if (address.startsWith("0x"))
			{
				base = 16;
				address = address.substring(2);
			}
			if (address.contains("+"))
			{
				int index = address.indexOf('+');
				String register = address.substring(index + 1).trim();
				_register = Emulator.Registers.getRegister(register);
				address = address.substring(0, index);
				useBoth = true;
				_value = (char)(_register.ordinal() + 0x10);
			}
			_address = (char) Integer.parseInt(address, base);
			useRegister = false;
		}
	}

	@Override
	public char getValue()
	{
		return 0;
	}

	@Override
	public void setValue(char in)
	{
	}

	@Override
	public int getCycleCost()
	{
		return 0;
	}
}
